package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public void update(User u) {
        userMapper.updateByPrimaryKey(u);
    }

    public PageInfo<User> list(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public User login(String email, String password) {
        User user = userMapper.selectByInfo(email, password);
        if (user == null) { //登录失败
            throw new RuntimeException("账号和密码不匹配");
        }
        return user;
    }

    public void regist(User u) {
        //检查用户是否存在
        String email = userMapper.selectByEmail(u.getEmail());
        if (email != null) {
            //当前用户已经注册
            throw new RuntimeException("该用户已经注册");
        }

        //先填充默认数据
        u.setPlace("亚洲");
        u.setGender(User.GENDER_NONE);
        u.setSign("还没有签名喔");
        u.setHeadImgUrl("/upload/head.jpg");
        u.setCoverImgUrl("/upload/bg.jpeg");
        //在保存到数据库
        userMapper.insert(u);
    }
}
